﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design.Serialization
Imports System.IO
Imports System.Reflection
Imports System.Runtime.Serialization.Formatters.Binary

Namespace System.Workflow.ComponentModel.Design
    Friend Class XomlComponentSerializationService
        Inherits ComponentSerializationService
        ' Methods
        Friend Sub New(ByVal serviceProvider As IServiceProvider)
            Me.serviceProvider = serviceProvider
        End Sub

        Public Overrides Function CreateStore() As SerializationStore
            Return New WorkflowMarkupSerializationStore(Me.serviceProvider)
        End Function

        Public Overrides Function Deserialize(ByVal store As SerializationStore) As ICollection
            If (store Is Nothing) Then
                Throw New ArgumentNullException("store")
            End If
            Dim store2 As WorkflowMarkupSerializationStore = [TryCast](store,WorkflowMarkupSerializationStore)
            If (store2 Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_UnknownSerializationStore"))
            End If
            Return store2.Deserialize(Me.serviceProvider)
        End Function

        Public Overrides Function Deserialize(ByVal store As SerializationStore, ByVal container As IContainer) As ICollection
            If (store Is Nothing) Then
                Throw New ArgumentNullException("store")
            End If
            If (container Is Nothing) Then
                Throw New ArgumentNullException("container")
            End If
            Dim store2 As WorkflowMarkupSerializationStore = [TryCast](store,WorkflowMarkupSerializationStore)
            If (store2 Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_UnknownSerializationStore"))
            End If
            Return store2.Deserialize(Me.serviceProvider, container)
        End Function

        Public Overrides Sub DeserializeTo(ByVal store As SerializationStore, ByVal container As IContainer, ByVal validateRecycledTypes As Boolean, ByVal applyDefaults As Boolean)
            If (store Is Nothing) Then
                Throw New ArgumentNullException("store")
            End If
            If (container Is Nothing) Then
                Throw New ArgumentNullException("container")
            End If
            Dim store2 As WorkflowMarkupSerializationStore = [TryCast](store,WorkflowMarkupSerializationStore)
            If (store2 Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_UnknownSerializationStore"))
            End If
            store2.DeserializeTo(Me.serviceProvider, container)
        End Sub

        Friend Shared Function GetProperty(ByVal type As Type, ByVal name As String, ByVal bindingFlags As BindingFlags) As PropertyInfo
            Dim property As PropertyInfo = Nothing
            Try 
                [property] = type.GetProperty(name, bindingFlags)
            Catch exception1 As AmbiguousMatchException
                Dim info2 As PropertyInfo
                For Each info2 In type.GetProperties(bindingFlags)
                    If info2.Name.Equals(name, StringComparison.Ordinal) Then
                        Return info2
                    End If
                Next
            End Try
            Return [property]
        End Function

        Public Overrides Function LoadStore(ByVal stream As Stream) As SerializationStore
            If (stream Is Nothing) Then
                Throw New ArgumentNullException("stream")
            End If
            Dim formatter As New BinaryFormatter
            Return DirectCast(formatter.Deserialize(stream), WorkflowMarkupSerializationStore)
        End Function

        Public Overrides Sub Serialize(ByVal store As SerializationStore, ByVal value As Object)
            If (store Is Nothing) Then
                Throw New ArgumentNullException("store")
            End If
            If (value Is Nothing) Then
                Throw New ArgumentNullException("value")
            End If
            Dim store2 As WorkflowMarkupSerializationStore = [TryCast](store,WorkflowMarkupSerializationStore)
            If (store2 Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_UnknownSerializationStore"))
            End If
            store2.AddObject(value)
        End Sub

        Public Overrides Sub SerializeAbsolute(ByVal store As SerializationStore, ByVal value As Object)
            Me.Serialize(store, value)
        End Sub

        Public Overrides Sub SerializeMember(ByVal store As SerializationStore, ByVal owningObject As Object, ByVal member As MemberDescriptor)
            If (store Is Nothing) Then
                Throw New ArgumentNullException("store")
            End If
            If (owningObject Is Nothing) Then
                Throw New ArgumentNullException("owningObject")
            End If
            If (member Is Nothing) Then
                Throw New ArgumentNullException("member")
            End If
            Dim store2 As WorkflowMarkupSerializationStore = [TryCast](store,WorkflowMarkupSerializationStore)
            If (store2 Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_UnknownSerializationStore"))
            End If
            store2.AddMember(owningObject, member)
        End Sub

        Public Overrides Sub SerializeMemberAbsolute(ByVal store As SerializationStore, ByVal owningObject As Object, ByVal member As MemberDescriptor)
            Me.SerializeMember(store, owningObject, member)
        End Sub


        ' Fields
        Private serviceProvider As IServiceProvider
    End Class
End Namespace

